package com.zenithmind.classroom.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.classroom.pojo.dto.ProgressUpdateDTO;
import com.zenithmind.classroom.pojo.vo.LearningAnalysisVO;
import com.zenithmind.classroom.pojo.vo.LearningProgressVO;
import com.zenithmind.classroom.pojo.vo.LearningStatisticsVO;
import com.zenithmind.classroom.service.LearningProgressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 学习进度控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "学习进度管理", description = "学习进度管理相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/learning-progress")
@RequiredArgsConstructor
public class LearningProgressController {

    private final LearningProgressService learningProgressService;

    @Operation(summary = "更新学习进度", description = "更新学习进度")
    @PostMapping("/update")
    public Result<Boolean> updateProgress(@Valid @RequestBody ProgressUpdateDTO updateDTO) {
        Boolean result = learningProgressService.updateProgress(updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "开始学习", description = "开始学习")
    @PostMapping("/start")
    public Result<Boolean> startLearning(
            @Parameter(description = "课程ID") @RequestParam String courseId,
            @Parameter(description = "章节ID") @RequestParam(required = false) String chapterId,
            @Parameter(description = "课时ID") @RequestParam(required = false) String lessonId) {
        String userId = UserContext.getUserId();
        Boolean result = learningProgressService.startLearning(userId, courseId, chapterId, lessonId);
        return Result.success(result);
    }

    @Operation(summary = "完成学习", description = "完成学习")
    @PostMapping("/complete")
    public Result<Boolean> completeLearning(
            @Parameter(description = "课程ID") @RequestParam String courseId,
            @Parameter(description = "章节ID") @RequestParam(required = false) String chapterId,
            @Parameter(description = "课时ID") @RequestParam(required = false) String lessonId) {
        String userId = UserContext.getUserId();
        Boolean result = learningProgressService.completeLearning(userId, courseId, chapterId, lessonId);
        return Result.success(result);
    }

    @Operation(summary = "暂停学习", description = "暂停学习")
    @PostMapping("/pause")
    public Result<Boolean> pauseLearning(
            @Parameter(description = "课程ID") @RequestParam String courseId,
            @Parameter(description = "章节ID") @RequestParam(required = false) String chapterId,
            @Parameter(description = "课时ID") @RequestParam(required = false) String lessonId) {
        String userId = UserContext.getUserId();
        Boolean result = learningProgressService.pauseLearning(userId, courseId, chapterId, lessonId);
        return Result.success(result);
    }

    @Operation(summary = "获取课程学习进度", description = "获取用户课程学习进度")
    @GetMapping("/course/{courseId}")
    public Result<LearningProgressVO> getUserCourseProgress(@Parameter(description = "课程ID") @PathVariable String courseId) {
        String userId = UserContext.getUserId();
        LearningProgressVO progress = learningProgressService.getUserCourseProgress(userId, courseId);
        return Result.success(progress);
    }

    @Operation(summary = "获取章节学习进度", description = "获取用户章节学习进度")
    @GetMapping("/chapter/{chapterId}")
    public Result<LearningProgressVO> getUserChapterProgress(@Parameter(description = "章节ID") @PathVariable String chapterId) {
        String userId = UserContext.getUserId();
        LearningProgressVO progress = learningProgressService.getUserChapterProgress(userId, chapterId);
        return Result.success(progress);
    }

    @Operation(summary = "获取课时学习进度", description = "获取用户课时学习进度")
    @GetMapping("/lesson/{lessonId}")
    public Result<LearningProgressVO> getUserLessonProgress(@Parameter(description = "课时ID") @PathVariable String lessonId) {
        String userId = UserContext.getUserId();
        LearningProgressVO progress = learningProgressService.getUserLessonProgress(userId, lessonId);
        return Result.success(progress);
    }

    @Operation(summary = "获取学习统计", description = "获取用户学习统计")
    @GetMapping("/statistics")
    public Result<LearningStatisticsVO> getUserLearningStatistics(
            @Parameter(description = "开始时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        String userId = UserContext.getUserId();
        LearningStatisticsVO statistics = learningProgressService.getUserLearningStatistics(userId, startTime, endTime);
        return Result.success(statistics);
    }

    @Operation(summary = "获取课程学习统计", description = "获取课程学习统计")
    @GetMapping("/course/{courseId}/statistics")
    public Result<LearningStatisticsVO> getCourseLearningStatistics(@Parameter(description = "课程ID") @PathVariable String courseId) {
        LearningStatisticsVO statistics = learningProgressService.getCourseLearningStatistics(courseId);
        return Result.success(statistics);
    }

    @Operation(summary = "获取学习分析", description = "获取用户学习分析")
    @GetMapping("/analysis")
    public Result<LearningAnalysisVO> getUserLearningAnalysis(
            @Parameter(description = "分析天数") @RequestParam(defaultValue = "30") Integer days) {
        String userId = UserContext.getUserId();
        LearningAnalysisVO analysis = learningProgressService.getUserLearningAnalysis(userId, days);
        return Result.success(analysis);
    }

    @Operation(summary = "获取学习排行榜", description = "获取学习排行榜")
    @GetMapping("/ranking")
    public Result<List<LearningProgressVO>> getLearningRanking(
            @Parameter(description = "课程ID") @RequestParam(required = false) String courseId,
            @Parameter(description = "排行类型") @RequestParam(defaultValue = "1") Integer type,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<LearningProgressVO> ranking = learningProgressService.getLearningRanking(courseId, type, limit);
        return Result.success(ranking);
    }

    @Operation(summary = "获取学习建议", description = "获取学习建议")
    @GetMapping("/recommendations")
    public Result<List<String>> getLearningRecommendations(
            @Parameter(description = "课程ID") @RequestParam String courseId) {
        String userId = UserContext.getUserId();
        List<String> recommendations = learningProgressService.getLearningRecommendations(userId, courseId);
        return Result.success(recommendations);
    }

    @Operation(summary = "记录学习行为", description = "记录学习行为")
    @PostMapping("/record-behavior")
    public Result<Boolean> recordLearningBehavior(
            @Parameter(description = "课程ID") @RequestParam String courseId,
            @Parameter(description = "课时ID") @RequestParam(required = false) String lessonId,
            @Parameter(description = "行为类型") @RequestParam String action,
            @Parameter(description = "持续时间") @RequestParam(required = false) Integer duration,
            @Parameter(description = "设备类型") @RequestParam(required = false) Integer deviceType) {
        String userId = UserContext.getUserId();
        Boolean result = learningProgressService.recordLearningBehavior(userId, courseId, lessonId, action, duration, deviceType);
        return Result.success(result);
    }

    @Operation(summary = "获取学习路径", description = "获取学习路径")
    @GetMapping("/path")
    public Result<List<LearningProgressVO>> getLearningPath(
            @Parameter(description = "课程ID") @RequestParam String courseId) {
        String userId = UserContext.getUserId();
        List<LearningProgressVO> path = learningProgressService.getLearningPath(userId, courseId);
        return Result.success(path);
    }

    @Operation(summary = "预测完成时间", description = "预测学习完成时间")
    @GetMapping("/predict-completion")
    public Result<LocalDateTime> predictCompletionTime(
            @Parameter(description = "课程ID") @RequestParam String courseId) {
        String userId = UserContext.getUserId();
        LocalDateTime completionTime = learningProgressService.predictCompletionTime(userId, courseId);
        return Result.success(completionTime);
    }

    @Operation(summary = "获取学习热力图", description = "获取学习热力图")
    @GetMapping("/heatmap")
    public Result<List<LearningAnalysisVO.HeatmapData>> getLearningHeatmap(
            @Parameter(description = "年份") @RequestParam(required = false) Integer year) {
        String userId = UserContext.getUserId();
        if (year == null) {
            year = LocalDateTime.now().getYear();
        }
        List<LearningAnalysisVO.HeatmapData> heatmap = learningProgressService.getLearningHeatmap(userId, year);
        return Result.success(heatmap);
    }

    @Operation(summary = "同步学习进度", description = "同步学习进度")
    @PostMapping("/sync")
    public Result<Boolean> syncLearningProgress(
            @Parameter(description = "设备类型") @RequestParam(required = false) Integer deviceType) {
        String userId = UserContext.getUserId();
        Boolean result = learningProgressService.syncLearningProgress(userId, deviceType);
        return Result.success(result);
    }

    @Operation(summary = "导出学习报告", description = "导出学习报告")
    @PostMapping("/export-report")
    public Result<String> exportLearningReport(
            @Parameter(description = "课程ID") @RequestParam String courseId,
            @Parameter(description = "导出格式") @RequestParam(defaultValue = "pdf") String format) {
        String userId = UserContext.getUserId();
        String reportPath = learningProgressService.exportLearningReport(userId, courseId, format);
        return Result.success(reportPath);
    }

    @Operation(summary = "重置学习进度", description = "重置学习进度")
    @PostMapping("/reset")
    public Result<Boolean> resetLearningProgress(
            @Parameter(description = "课程ID") @RequestParam String courseId) {
        String userId = UserContext.getUserId();
        Boolean result = learningProgressService.resetLearningProgress(userId, courseId);
        return Result.success(result);
    }

    @Operation(summary = "获取学习提醒", description = "获取学习提醒")
    @GetMapping("/reminders")
    public Result<List<String>> getLearningReminders() {
        String userId = UserContext.getUserId();
        List<String> reminders = learningProgressService.getLearningReminders(userId);
        return Result.success(reminders);
    }
}
